import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Users from '../components/users/users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Report from '../components/report/report.vue'
import Order from '../components/orders/order.vue'
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/reports', component: Report },
      { path: '/orders', component: Order }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 限制访问权限
// 路由对象
router.beforeEach((to, from, next) => {
  // to要访问的路径
  // next表示放行
  // 如果用户访问登录页，则直接放行
  if (to.path === '/login') return next()
  // 获取token
  // 从sessionstorage中获取保存的token值
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果没有token值，强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})
export default router
